<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>登录</title>
  <link rel="stylesheet" href="./bootstrap/bootstrap.min.css" />
  <link rel="stylesheet" href="./bootstrap/bootstrap-icons.css" />
  <link rel="stylesheet" href="./css/common.css" />

</head>

<body class="application application-offset">
  <div class="container-fluid container-application">
    <div class="main-content position-relative">
      <!-- Page content -->
      <div class="page-content">
        <div class="min-vh-100 py-5 d-flex align-items-center">
          <div class="w-100">
            <div class="row justify-content-center">
              <div class="col-sm-8 col-lg-4">
                <div class="card shadow zindex-100 mb-0">
                  <div class="card-body px-md-5 py-5">
                    <div class="mb-5">
                      <h6 class="h3">传智教育-注册</h6>
                    </div>
                    <span class="clearfix"></span>
                    <!-- 注册表单 -->
                    <form role="form" autocomplete="off" id="registerForm">
                      <div class="form-group">
                        <label class="form-control-label">账号</label>
                        <div class="input-group input-group-merge">
                          <div class="input-group-prepend">
                            <span class="input-group-text"><i class="bi bi-person"></i></span>
                          </div>
                          <input name="username" type="text" class="form-control" id="input-email"
                            placeholder="请输入账号" />
                        </div>
                      </div>
                      <div class="form-group mb-4">
                        <div class="d-flex align-items-center justify-content-between">
                          <div>
                            <label class="form-control-label">密码</label>
                          </div>
                          <div class="mb-2">
                            <a href="javascript:;"
                              class="small text-muted text-underline--dashed border-primary">忘记密码</a>
                          </div>
                        </div>
                        <div class="input-group input-group-merge">
                          <div class="input-group-prepend">
                            <span class="input-group-text"><i class="bi bi-key"></i></span>
                          </div>
                          <input name="password" type="password" class="form-control" id="input-password"
                            placeholder="请输入密码" />
                          <div class="input-group-append">
                            <span class="input-group-text">
                              <a href="javascript:;" data-toggle="password-text" data-target="#input-password">
                                <i class="bi bi-eye-fill text-blue"></i>
                              </a>
                            </span>
                          </div>
                        </div>
                      </div>
                      <div class="mt-4">
                        <button id="btn-register" type="button"
                          class="btn btn-sm btn-info btn-icon btn-blue rounded-pill">
                          <span class="btn-inner--text">注册</span>
                          <span class="btn-inner--icon"><i class="bi bi-arrow-right"></i></span>
                        </button>
                      </div>
                    </form>
                  </div>
                  <div class="card-footer px-md-5">
                    <small>已经注册？</small> <a href="./login.html" class="small font-weight-bold text-blue">去登录吧</a>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <!-- toast -->
  <div class="position-fixed top-0 start-50 pt-4" style="z-index: 999; transform: translateX(-50%)">
    <div id="myToast" class="toast bg-rgba">
      <div class="toast-body">提示消息</div>
    </div>
  </div>
  <script src="./bootstrap/bootstrap.min.js"></script>
  <script src="./lib/form-serialize.js"></script>
  <script src="./lib/axios.js"></script>
  <script src="./js/common.js"></script>

</body>
<script>
  //配置ajax请求基地址
  axios.defaults.baseURL = 'http://ajax-api.itheima.net'
  // 创建bootstrap弹窗
  let toastBox = document.querySelector('#myToast')
  const toast = new bootstrap.Toast(toastBox, {
    animate: true,
    autohide: true,
    delay: 3000 //持续时间
  })
  //toast.show()调用即可

  // 补充新知识
  //form标签内 无论是button按钮，还是input按钮，都可以点击提交
  //button[type=button],它点击不会触发提交行为(默认的type值为submit，会提交)
  //input[type=button],它也不会触发提交行为(input[type=submit]按钮，会触发提交)

  //补充知识点
  //只要是表单，就要验证合法性(防止被注入攻击：防止用户在表单中写入代码影响项目运行)

  //1.给注册按钮绑定点击事件
  document.querySelector('#btn-register').addEventListener('click', async e => {
    //2.获取用户表单输入的内容(通过form-serialize.js插件)
    let formObj = serialize(document.querySelector('#registerForm'), { hash: true })
    console.log(formObj);

    //3.我们要对表单域内容进行判断
    //数据结构
    //语法：let {key:变量名} = {key:值}
    //注意：如果解构失败，左边的变量变为undefined
    //解决：在解构时给变量一个默认值，乳沟有值，变量用值，否则才用默认值
    let { username = '', password = '' } = formObj

    //4.对表单域的内容进行校验
    if (username.length < 2 || username.length > 30) {
      //错误提示
      toastBox.innerHTML = '账号在2-30个字符'
      toast.show()
    } else if (password.length < 6 || password.length > 30) {
      //错误提示
      toastBox.innerHTML = '密码在6-30个字符'
      toast.show()
    } else {
      //通过校验，准备发请求
      try {
        await axios({
          url: './register',
          method: 'post',
          data: formObj
        }).then(res => {
          //成功回调
          console.log(res)
          //成功后给用户提示
          // if (res.status == 201) {
          toastBox.innerHTML = res.data.message
          toast.show()
          //注册成功后跳转到登录页面
          location.href = './login.html'
          // }
        });
      } catch (err) {
        console.log(err);
        //用户名重复提示
        if (err.response.status == 409) {
          toastBox.innerHTML = err.response.data.message
          toast.show()
        }
      }
    }

  })
</script>

</html>